package cn.jhc.servlet;

import cn.jhc.beans.SurveyItem;
import cn.jhc.db.SurveyItemDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;

@WebServlet("/survey.do")
public class SaveItemServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        SurveyItem form = new SurveyItem();
        String name = req.getParameter("name");
        form.setName(name);
        String ageString = req.getParameter("age");
        int age = Integer.parseInt(ageString);
        form.setAge(age);
        String gender = req.getParameter("gender");
        form.setGender(gender);
        String occupation = req.getParameter("occupation");
        form.setOccupation(occupation);
        String[] hobbies = req.getParameterValues("hobbies");
        form.setHobbies(hobbies);

        Connection connection = (Connection) req.getServletContext().getAttribute("connection");
        SurveyItemDao.save(connection, form);
        
//        req.getRequestDispatcher("/WEB-INF/jsp/survey.jsp").forward(req, resp);
        resp.sendRedirect("list.jsp");
    }
}
